package com.biostime.qdingding.modules.qd.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.modules.qd.dao.StudentpromsDao;
import com.biostime.qdingding.modules.qd.entity.Studentproms;
import com.google.common.collect.Maps;

/**
 * 学员套餐Service
 * @author king
 * @version 2015-12-09
 */
@Service
@Transactional(readOnly = true)
public class StudentpromsService extends CrudService<StudentpromsDao, Studentproms> {
	
	public List<Studentproms> getbySetIdStudentId(Studentproms studentproms){
		
		return dao.getbySetIdStudentId(studentproms);
	}

	public boolean exist(Studentproms studentproms) {
		return dao.exist(studentproms);
	}
	
	public Map<String, String> findMapsByStudentId(Studentproms studentproms){
		Map<String, String> map = Maps.newHashMap();
		List<Studentproms> list = dao.findListByStudentId(studentproms);
		if(!list.isEmpty()){
			for(Studentproms temp : list){
				if( !map.containsKey(temp.getSetId()) ){
					map.put(String.valueOf(temp.getSetId()), "1");
				}
			}
		}
		
		return map;
	}
	
	public void save(Studentproms studentproms,HttpServletRequest request){
		int studentId = studentproms.getStudentId();
		dao.deleteByStudent(studentId);
		String[] studentpromsArray = request.getParameterValues("promotionset");
		if( studentpromsArray != null && studentpromsArray.length >0){
		for(String temp : studentpromsArray){
			Studentproms tempStudentproms = new Studentproms();
			tempStudentproms.setSetId(Integer.valueOf(temp));
			tempStudentproms.setStudentId(studentId);
			save(tempStudentproms);
		}
		}
	}
}
